//
//  PopoverPresentationController.swift
//  WeiBoSwift
//
//  Created by rong on 15/6/29.
//  Copyright © 2015年 rong. All rights reserved.
//

import UIKit


//ios 8.0推出的，专门用于  视图控制器之间的转场
class PopoverPresentationController: UIPresentationController {

    //展现的视图的  frame
    var presentFrame = CGRectZero
    
    /**
    containerView 容器视图，放置展现出来的视图的
    presentedView() 被展现出来的视图的
    
    */
    //presentedViewController:  被展现的控制器
    //presentingViewController：发起的控制器
    //
    override init(presentedViewController: UIViewController, presentingViewController: UIViewController) {
        super.init(presentedViewController: presentedViewController, presentingViewController: presentingViewController)
        
        print(containerView)//此时为nil, 因为还没有显示
        
        clickMaskView()
    }
    
    //func containerViewWillLayoutSubviews(): 容器视图，将要布局子视图
    //func containerViewDidLayoutSubviews()
    //
    override func containerViewWillLayoutSubviews(){
    
        super.containerViewWillLayoutSubviews()
        
        maskView.frame = containerView!.bounds
        containerView?.insertSubview(maskView, atIndex: 0)
        
        presentedView()?.frame = presentFrame//CGRectMake(100, 56, 200, 240)
        
    }
    
    // 遮罩视图
    lazy var maskView: UIView = {
    
        let v = UIView()
        v.backgroundColor = UIColor(white: 0.0, alpha: 0.2)
        
        return v
    }()
    
    //点击遮罩视图
    func clickMaskView()
    {
        let tap = UITapGestureRecognizer(target: self, action: "close")
        maskView.addGestureRecognizer(tap)
    }
    
    
    func close() {
        presentedViewController.dismissViewControllerAnimated(true, completion: nil)
    }
}
